package com.fcgzs.question.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fcgzs.question.entity.TypeEntity;
import com.fcgzs.question.service.TypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fcgzs.question.entity.QuestionEntity;
import com.fcgzs.question.service.QuestionService;
import com.fcgzs.common.utils.PageUtils;
import com.fcgzs.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author ljj
 * @email ljj@gmail.com
 * @date 2021-08-19 11:39:55
 */
@RestController
@RequestMapping("question/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private TypeService typeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("question:question:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("question:question:info")
    public R info(@PathVariable("id") Long id){
		QuestionEntity question = questionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("question:question:save")
    public R save(@RequestBody QuestionEntity question){
		questionService.save(question);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("question:question:update")
    public R update(@RequestBody QuestionEntity question){
		questionService.updateById(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("question:question:delete")
    public R delete(@RequestBody Long[] ids){
		questionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/importExcel")
    public R importExcel(@RequestParam(name = "file") MultipartFile multipartFile){
        Map<String, Object> map = questionService.importExcel(multipartFile);
        return R.ok().put("result",map.get("result")).put("msg",map.get("msg")).put("num",map.get("num"));
    }


    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        Workbook workbook = questionService.exportExcel();
        if(workbook!=null){
            String fileName = "kuxue_"+new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            try {
                OutputStream outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/countTypeQuestion")
    public R countTypeQuestion(){
        List<Map<String, Object>> mapList = questionService.countTypeQuestion();

        for (Map<String, Object> map : mapList) {
            TypeEntity typeEntity = typeService.getById((Long)map.get("name"));
            map.put("name",typeEntity.getType());
        }

        return R.ok().put("data",mapList);

    }

}
